import Common from '../../../common/common';
let common = new Common;
const app = getApp();
let timer;
Page({
  data: {
    currentTab: 9,
    navScrollLeft: 0,
    winWidth: 0,
    winHeight: 0,
    lssuingRecordList: [],
    popup: false, 
    pageNumber:1,
    dateStart: '',
    dateEnd: '',
    keywords: '', //搜索条件
    couponSeq: "", //筛选券号
    total:0,
    type:2,
    employeeName:'',//员工名字
    page:1,
    isEnd: false, //是否到底 
  },
  // 搜索条件
  bindKeyWords: function(e) {
    this.setData({
      keywords: e.detail.value
    })
    this.getGoodsTypeList(1,1);
  },
  // 筛选券号
  bindKeyNo: function(e) {
    this.setData({
      couponSeq: e.detail.value
    })
  }, 
  onLoad: function(options) {
    console.log(options)
    let that = this
    that.setData({
      employeeName:options.employeeName
    })
    console.log(options.employeeName)
    that.getGoodsTypeList(1,1) //调用活动管理列表信息接口
  },

  // 获取活动管理列表信息
  getGoodsTypeList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中',
    }) 
    if(this.data.dateStart == '请选择'){
      that.setData({
        dateStart:'',
      })
    }
    if(this.data.dateEnd == '请选择'){
      that.setData({
        dateEnd:'',
      })
    }
    let data = {
      condition: { 
        orgSeq: app.globalData.orgSeq, //orgSeq 门店id
        keywords: this.data.keywords,
        beginDate: this.data.dateStart, 
        endDate: this.data.dateEnd,
        couponStatus: this.data.currentTab, //全部9，待使用1，已使用2，已失效3
        couponType: '',
        couponSeq: this.data.couponSeq,
        employeeName:this.data.employeeName
      },
      current: this.data.page,
      size: 10,
    }
    common.requestAjax('/api/promotion-service/v1/promotionCoupon/queryMemberCouponRecordList', 'POST', data).then(res => {
      if (res.data.code == 1) {
        wx.hideLoading() 
        let list = this.data.lssuingRecordList || [];  
        let isEnd = this.data.isEnd;
        if ((res.data.records.length == 0 )|| (res.data.records.length <  10 && this.data.page == 1)) { 
          isEnd = true;//最后一行没数据 到底了
        } 
        list = list.concat(res.data.records); 
        console.log(list)
        this.setData({
          lssuingRecordList: list, 
          isEnd,
          total:res.data.total ,//合计数
        })
      }else{
        common.showToast(res.data.message)
      }
    }).catch(err => {
      wx.hideLoading();
    })
  },
  // 点击标题切换当前页时改变样式
  switchNav: function(e) { 
    var that = this
    var cur = e.currentTarget.dataset.current;
    console.log(e)
    that.setData({
      currentTab: cur,
      dateStart:'',
      couponSeq:'',
      dateEnd:'',
      page:1,
      isEnd:false,
      lssuingRecordList:[],
    }) 
    that.getGoodsTypeList(1,1)
  },
  // 滚动切换标签样式 
  switchTab: function(e) {
    console.log(e)
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  // 时间选择
  dateStart: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      dateStart: e.detail.value
    })
  },
  dateEnd: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      dateEnd: e.detail.value
    })
  },
  // 筛选弹出
  screenBox: function() {
    this.setData({
      popup: true,
      dateStart: '请选择',
      dateEnd: '请选择',
      couponSeq:''
    })
  },
  // 操作弹窗    确定 取消
  operation: function(e) {
    console.log(e.currentTarget.dataset.id)
    if (e.currentTarget.dataset.id == 1) { //1是点击确定  
    // 开始时间戳
      var dateStart = this.data.dateStart.replace(/-/g, '/');
      var dateStartTamp = Date.parse(dateStart);
      console.log('开始时间戳=====' + dateStartTamp)
      // 结束时间戳
      var dateEnd = this.data.dateEnd.replace(/-/g, '/');
      var dateEndTamp = Date.parse(dateEnd);
      if (dateStartTamp > dateEndTamp){
        wx.showToast({
          title:'开始时间不能大于结束时间',
          icon:'none'
        })
        return;
      }
      this.setData({
        popup: false
      })
      this.getGoodsTypeList(1,1);
      console.log('结束时间戳=====' + dateEndTamp)  
      if (this.data.dateStart == '请选择' || this.data.dateEnd == '请选择') {
        
      }
    } else { // 0 点击取消
      this.setData({
        popup: false
      })
    }
  }, 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getGoodsTypeList(1,1);
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1200)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (!this.data.isEnd) { 
      wx.showLoading({
        title: '正在加载中...',
      })
      let page = this.data.page;
      page = page+1
      this.setData({
        page:page
      }) 
      this.getGoodsTypeList();
    }
  }
})